package com.hqj.util;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hqj.entity.TMenu;
import com.hqj.entity.TRole;
import com.hqj.entity.TUser;
import com.hqj.service.TRoleServiceImpl;
import com.hqj.service.TUserServiceImpl;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

import javax.annotation.Resource;
import java.util.List;

/**
 * 自定义登录验证及权限验证
 */
public class MyRealm extends AuthorizingRealm {

    @Resource(name="TUserServiceImpl")
    private TUserServiceImpl tsi;
    @Resource(name="TRoleServiceImpl")
    private TRoleServiceImpl rsi;
    /**
     * 角色权限  通过AuthorizationInfo授权   - 将数据库中角色对应的权限授权给登录的用户
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {

        //获取登录名称
        String account = principalCollection.getPrimaryPrincipal()+"";
        //通过登录名去数据库查询用户信息
        QueryWrapper<TUser> query = new QueryWrapper<>();
        query.eq(AccountUtil.getLoginType(account),account);
        TUser t = tsi.getOne(query);

        List<TRole> roles = rsi.findUserRoleMenu(t.getId());
        //把角色与权限记录到shiro中
        SimpleAuthorizationInfo simpleAuthorizationInfo =
                new SimpleAuthorizationInfo();//权限信息
        for(TRole trole:roles){
            //将所有角色信息添加
            simpleAuthorizationInfo.addRole(trole.getName());
            for(TMenu menu:trole.getMenus()){
                //添加所有对应的权限信息
                simpleAuthorizationInfo.addStringPermission(menu.getAuthority());
            }
        }

        return simpleAuthorizationInfo;
    }


    /**
     * 用户身份验证  验证用户是否存在及密码是否正确，需要查询数据库
     * @param token
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        //加上这句是为了防止post请求会先进入认证再到请求
        if(token.getPrincipal()==null){
            return null;
        }

        //从token中获取用户名(网页post提交的登录名)
        String account = token.getPrincipal().toString();
        //通过登录名去数据库查询用户信息
        QueryWrapper<TUser> query = new QueryWrapper<>();
        query.eq(AccountUtil.getLoginType(account),account);
        TUser t = tsi.getOne(query);
        if(t==null){
            //登录名错误
            return null;
        }else{
            //这里验证密码  通过AuthenticationToken 和 SimpleAuthenticationInfo验证信息
            SimpleAuthenticationInfo simpleAuthenticationInfo =
                    new SimpleAuthenticationInfo(account,t.getPassword().toString(),getName());//getName表示当前类
            return simpleAuthenticationInfo;
        }

    }
}
